package com.zzsy.shoppingpro.shoppingcart.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.base.BaseFragment;
import com.zzsy.shoppingpro.home.acitivty.HomeActivity;
import com.zzsy.shoppingpro.home.bean.GoodsBean;
import com.zzsy.shoppingpro.shoppingcart.adapter.ShopCartAdapter;
import com.zzsy.shoppingpro.shoppingcart.utils.CartProvider;
import com.zzsy.shoppingpro.utils.Toast;

import java.util.List;

/**
 * Created by Administrator on 2019/8/17 0017.
 */

public class ShoppingCartFragment extends BaseFragment {


    private RecyclerView recyclerView;
    private CheckBox checkboxAll;
    private TextView tvShopcartTotal;
    private Button btnCheckOut;
    private TextView tvShopcartEdit;
    private LinearLayout llCheckAll;
    private LinearLayout llDelete;
    private CheckBox cbAll;
    private Button btnDelete;
    private Button btnCollection;
    private TextView tvEmptyCartTobuy;
    private LinearLayout ll_empty_shopcart;

    /**
     * 编辑状态
     * */
    private static final int ACTION_EDIT = 0;
    /**
     * 完成状态
     * */
    private static final int ACTION_COMPLETE = 1;

    private ShopCartAdapter adapter;


    @Override
    public View initView() {
        View view = View.inflate(context, R.layout.fragment_shoppingcart, null);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        //编辑完成切换按钮
        tvShopcartEdit = view.findViewById(R.id.tv_shopcart_edit);
        //列表控件
        recyclerView = view.findViewById(R.id.recyclerview);
        //全选结算按钮
        checkboxAll = view.findViewById(R.id.checkbox_all);
        //合计
        tvShopcartTotal = view.findViewById(R.id.tv_shopcart_total);
        //去结算
        btnCheckOut = view.findViewById(R.id.btn_check_out);
        //结算布局
        llCheckAll = view.findViewById(R.id.ll_check_all);
        //删除布局
        llDelete = view.findViewById(R.id.ll_detele);
        //全选删除
        cbAll = view.findViewById(R.id.cb_all);
        //删除按钮
        btnDelete = view.findViewById(R.id.btn_delete);
        //收藏按钮
        btnCollection = view.findViewById(R.id.btn_collection);
        //无数据显示布局
        tvEmptyCartTobuy = view.findViewById(R.id.tv_empty_cart_tobuy);
        ll_empty_shopcart = view.findViewById(R.id.ll_empty_shopcart);
        tvEmptyCartTobuy.setClickable(true);
    }

    @Override
    public void initData() {

        initListener();
        tvShopcartEdit.setTag(ACTION_EDIT);
        tvShopcartEdit.setText("编辑");
        llCheckAll.setVisibility(View.VISIBLE);
        showData();
    }

    @Override
    public void onResume() {
        super.onResume();
        showData();
    }

    /**
     * 监听事件
     * */
    private void initListener() {

        btnCheckOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.show(getActivity(),"进行支付");
            }
        });
        tvShopcartEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置编辑的点击事件
                int tag = (int) tvShopcartEdit.getTag();
                if(tag == ACTION_EDIT){
                    //变成完成状态
                    showDelete();
                }else{
                    //变成编辑状态
                    hideDelete();
                }
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.deleteData();
                adapter.showTotalPrice();
                //显示空空如也
                checkData();
                adapter.checkAll();
            }
        });

        tvEmptyCartTobuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeActivity.class);
                startActivity(intent);
            }
        });

    }

    /**
     * 变成编辑状态
     * */
    private void hideDelete() {
        tvShopcartEdit.setText("编辑");
        tvShopcartEdit.setTag(ACTION_EDIT);

        adapter.checkAll_none(true);
        llDelete.setVisibility(View.GONE);
        llCheckAll.setVisibility(View.VISIBLE);

        adapter.showTotalPrice();
    }
    /**
     * 变成完成状态
     * */
    private void showDelete() {
        tvShopcartEdit.setText("完成");
        tvShopcartEdit.setTag(ACTION_COMPLETE);

        adapter.checkAll_none(false);
        cbAll.setChecked(false);
        checkboxAll.setChecked(false);

        llDelete.setVisibility(View.VISIBLE);
        llCheckAll.setVisibility(View.GONE);

        adapter.showTotalPrice();
    }
    /**
     * 显示空空如也
     * */
    private void checkData() {
        if(adapter != null && adapter.getItemCount() > 0){
            tvShopcartEdit.setVisibility(View.VISIBLE);
            ll_empty_shopcart.setVisibility(View.GONE);
        }else{
            ll_empty_shopcart.setVisibility(View.VISIBLE);
            tvShopcartEdit.setVisibility(View.GONE);
        }
    }

    private void showData() {

        CartProvider cartProvider = CartProvider.getInstance();
        List<GoodsBean> datas = cartProvider.getDataFromLocal();
        if(datas != null &&datas.size() > 0){
            tvShopcartEdit.setVisibility(View.VISIBLE);
            adapter = new ShopCartAdapter(context,datas,tvShopcartTotal,cartProvider,checkboxAll,cbAll);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(adapter);
            ll_empty_shopcart.setVisibility(View.GONE);
        }else{
            //显示空的
            tvShopcartEdit.setVisibility(View.GONE);
            ll_empty_shopcart.setVisibility(View.VISIBLE);
        }

    }

}
